
#include <dynamic_buffer.hpp>

#include <string>

using namespace xbase;

int main()
{

	std::string DATA("1234567890ABCDEF");
	{
		ref_buffer::pointer buf1 = ref_buffer::allocate(DATA.size());
		assert(buf1->size() == DATA.size());
		assert(boost::asio::buffer_size(buf1->data()) == DATA.size());

		std::memcpy(buf1->get(), DATA.data(), DATA.size());
	}
	// release buf1

	std::string data("1234567890abcdef");
	ref_buffer::pointer buf;
	{
		ref_buffer::pointer buf2 = ref_buffer::allocate(data.size());
		assert(buf2->size() == data.size());
		assert(boost::asio::buffer_size(buf2->data()) == data.size());

		std::memcpy(buf2->get(), data.data(), data.size());
		buf = buf2;	// hold it
	}

	char *released_buffer;
	{
		ref_buffer::pointer buf3 = ref_buffer::allocate(DATA.size());
		assert(buf3->size() == DATA.size());
		assert(boost::asio::buffer_size(buf3->data()) == DATA.size());

		std::memcpy(buf3->get(), DATA.data(), DATA.size());
		released_buffer = buf3->get();
	}
	// release buf3

	std::string store(buf->get(), buf->size());
	assert(store == data);
	assert(store != DATA);
	
	// release buf2
	return 0;
}
